<?php

namespace app\admin\controller\system;

use app\admin\model\SystemBranch;
use app\admin\traits\Curd;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="system_branch")
 */
class Branch extends AdminController
{

    use Curd;


    protected $allowModifyFields = [
        'name', 'brief', 'sort'
    ];

    protected $sort = [
        'sort' => 'desc'
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new SystemBranch();
        
    }


    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($branch_id)
    {
        $row = $this->model->find($branch_id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="删除")
     */
    public function delete($branch_id)
    {
        $row = $this->model->whereIn('id', $branch_id)->select();
        $row->isEmpty() && $this->error('数据不存在');
        try {
            $save = $row->delete();
        } catch (\Exception $e) {
            $this->error('删除失败');
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }



}